#include "muduo/proto/dispatcher.h"
#include "muduo/proto/codec.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/net/TcpClient.h"
#include "muduo/base/CountDownLatch.h"

#include "request.pb.h"
#include <iostream>

class Client
{
    using Message = google::protobuf::Message;
    using MessagePtr = std::shared_ptr<google::protobuf::Message>;
    using TranslateResponsePtr = std::shared_ptr<request::TranslateResponse>;
    using AddResponsePtr = std::shared_ptr<request::AddResponse>;
public:
    Client(const std::string& svr_ip, uint16_t svr_port) 
        : latch_(1)
        , client_(loopthread_.startLoop(), muduo::net::InetAddress(svr_ip, svr_port), "Client")
        , dispatcher_(std::bind(&Client::onUnknownMessage, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
        , codec_(std::bind(&ProtobufDispatcher::onProtobufMessage, &dispatcher_,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        dispatcher_.registerMessageCallback<request::TranslateResponse>(std::bind(&Client::onTranslate, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        dispatcher_.registerMessageCallback<request::AddResponse>(std::bind(&Client::onAdd, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        client_.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &codec_,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        client_.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1));
    }

    void connect()
    {
        client_.connect();
        latch_.wait();
    }

    void translate(const std::string& msg)
    {
        auto req = std::make_shared<request::TranslateRequest>();
        req->set_msg(msg);
        send(req);
    }

    void add(int num1, int num2)
    {
        auto req = std::make_shared<request::AddRequest>();
        req->set_num1(num1);
        req->set_num2(num2);
        send(req);
    }

private:
    bool send(const MessagePtr& message)
    {
        if(conn_->connected() != true) return false;
        codec_.send(conn_, *message);
        return true;
    }

    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected())
        {
            conn_ = conn;
            latch_.countDown();
        }
        else conn_.reset();
    }

    void onTranslate(const muduo::net::TcpConnectionPtr& conn,
                    const TranslateResponsePtr& message, muduo::Timestamp)
    {
        std::cout << "translate reuslt: " << message->msg() << "\n";
    }

    void onAdd(const muduo::net::TcpConnectionPtr& conn,
                const AddResponsePtr& message, muduo::Timestamp)
    {
        std::cout << "num1 + num2 = " << message->result() << "\n";
    }

    void onUnknownMessage(const muduo::net::TcpConnectionPtr&,
                        const MessagePtr& message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
    }

private:
    muduo::CountDownLatch latch_;
    muduo::net::EventLoopThread loopthread_;
    muduo::net::TcpConnectionPtr conn_;
    muduo::net::TcpClient client_;
    ProtobufDispatcher dispatcher_;
    ProtobufCodec codec_;
};

int main()
{
    Client client("127.0.0.1", 8888);
    client.connect();

    client.translate("hello");
    client.add(1, 2);
    sleep(1);
    return 0;
}